package com.zut.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zut.commonutils.R;
import com.zut.eduservice.entity.Usertable;
import com.zut.eduservice.entity.vo.ClassInfo;
import com.zut.eduservice.service.UsertableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-12-18
 */
@RestController
@RequestMapping("/eduservice/usertable")
@CrossOrigin
public class UsertableController {


    @Autowired
    private UsertableService usertableService;

    //添加课程分类
    //获取上传过来文件，把文件内容读取出来
    @PostMapping("addSubject")
    public R addSubject(MultipartFile file,ClassInfo classInfo) {
        //上传过来excel文件
        usertableService.saveUser(file,usertableService,classInfo);

        return R.ok();
    }
    @PostMapping("deliver")
    public R deliver(  @RequestBody ClassInfo classInfo) {
      //用于传递参数
        usertableService.deliverUser(classInfo);
        return R.ok();
    }

    //查询被标记学生信息
    @GetMapping("getStudentInfoemation/{current}/{limit}")
    public R getStudentInformation(@PathVariable long current, @PathVariable long limit
                          ){
        Page<Usertable> pageInformation = new Page<>(current,limit);
        //构建条件
        QueryWrapper<Usertable> wrapper =new QueryWrapper<>();
        wrapper.like("sign",1);
        usertableService.page(pageInformation,wrapper);
        long total = pageInformation.getTotal();
        List<Usertable> records= pageInformation.getRecords();
        return R.ok().data("total",total).data("records",records);
    }

    //标记学生
    @PostMapping("updataSign/{id}")
    public R updataSign( @PathVariable int id){
        Usertable usertable =new Usertable();
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id );
        updateWrapper.set("sign", 1);
        usertableService.update(usertable,updateWrapper);
       return R.ok();
    }

    //取消标记
    @PostMapping("undoUpdataSign/{id}")
    public R undoUpdataSign(@PathVariable int id){
        Usertable usertable =new Usertable();
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id );
        updateWrapper.set("sign", 0);
        usertableService.update(usertable,updateWrapper);
        return R.ok();
    }

    //条件分页查询
    @PostMapping("pageInformationCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) Usertable usertable) {
        //创建page对象
        Page<Usertable> pageInformation = new Page<>(current,limit);

        //构建条件
        QueryWrapper<Usertable> wrapper =new QueryWrapper<>();
        String studentName =usertable.getStudentName();
        String studentId =usertable.getStudentId();
        String className =usertable.getClassName();
        String calssId =usertable.getClassId();
        if(!StringUtils.isEmpty(studentName)){
            wrapper.like("student_name",studentName);

        }
        if(!StringUtils.isEmpty(studentId)){
            wrapper.eq("student_id",studentId);
        }
        if(!StringUtils.isEmpty(className)){
            wrapper.like("class_name",className);
        }
        if(!StringUtils.isEmpty(calssId)){
            wrapper.eq("class_id",calssId);
        }

        //调用方法实现条件查询分页
        usertableService.page(pageInformation,wrapper);
        long total = pageInformation.getTotal();
        List<Usertable> records= pageInformation.getRecords();
        return R.ok().data("total",total).data("records",records);

    }
    //根据id查询被标记学生信息为上传证据做铺垫
    @GetMapping("getUploadInformmation/{id}")
    public R getUploadInformation(@PathVariable int id){
       Usertable usertable= usertableService.getById(id);
        return R.ok().data("records",usertable);
    }

}

